<?php

namespace app\mobile\controller;

class News extends Common {

        function _initialize() {
                parent::_initialize();
                if (!empty($_REQUEST['seo_string'])) {
                        $find_sort = D("NewsCategory")->where(" write_string = '" . $_REQUEST['seo_string'] . "'")->find();
                        $find_sort2 = D("ToursCategory")->where(" write_string = '" . $_REQUEST['zonepinyin'] . "'")->find();
                        $_REQUEST['c'] = $find_sort['id'];
                        $_REQUEST['zone'] = $find_sort2['title'];
                }
        }

        public function show_news() {
                $id = I("get.id");
                //读取资讯
                $Article = D("tours/News");
                $map = array();
                $map['id'] = $id;
                $map['ischecked'] = 1;

                $Article->where($map)->setInc('hits');
                $arc = $Article->relation(true)->where($map)->find();
                if (empty($arc)) {
                        $this->_empty();
                        return;
                }
                $this->assign('arc', $arc);

                //上一篇
                $last = $Article->where(array("id" => array("lt", $id), "ischecked" => 1))->limit(1)->order("id desc")->select();
                if (!empty($last)) {
                        $this->assign("last", $last[0]);
                }

                //下一篇
                $next = $Article->where(array("id" => array("gt", $id), "ischecked" => 1))->limit(1)->select();
                if (!empty($next)) {
                        $this->assign("next", $next[0]);
                } else {
                        $this->assign('next', '');
                }

                if (in_array($arc['prov'], array(21, 302, 327, 385))) {
                        $cid = $arc['prov'];
                        $data['prov'] = $arc['prov'];
                } else {
                        $cid = $arc['city'];
                        $data['city'] = $arc['city'];
                }

                //相关资讯
                $data['id'] = array('neq', $id);
                $data['ischecked'] = 1;
                $relist = $Article->where($data)->order('rand()')->limit(6)->select();
                $this->assign('relist', $relist);

                //最新资讯
                $zxlist = $Article->where(array('ischecked' => 1, 'cid' => $arc['cid']))->order('postdate desc')->limit(10)->select();
                $this->assign('zxlist', $zxlist);

                //SEO优化，生成TITLE,K,D
                $from = array('，', '{title}', '{content}');
                $to = array(',', $arc['subject'], msubstr(($arc['content']), 0, 120));
                if (!empty($arc['seo_title'])) {
                        $seo_title = $arc['seo_title'];
                } else {
                        $seo_title = str_replace($from, $to, C('site.news_seotitle'));
                }
                if (!empty($arc['seo_keywords'])) {
                        $seo_keywords = $arc['seo_keywords'];
                } else {
                        $seo_keywords = str_replace($from, $to, C('site.news_seokeywords'));
                }
                if (!empty($arc['seo_description'])) {
                        $seo_description = $arc['seo_description'];
                } else {
                        $seo_description = str_replace($from, $to, C('site.news_seodescription'));
                }
                $this->assign('seo_title', $seo_title);
                $this->assign('seo_keywords', $seo_keywords);
                $this->assign('seo_description', $seo_description);

                return $this->fetch();
        }

        public function newslist() {

                $write_string = I("get.write_string");
                $cate = M('NewsCategory')->field(true)->where(array('write_string' => $write_string))->cache()->find();
                if (empty($cate)) {
                        $this->_empty();
                        return;
                }
                $this->assign('cate', $cate);

                //列表
                
                $news_info = D('tours/News');
                $where['ischecked'] = 1;
                $where['cid'] = $cate['id'];
                $count = $news_info->where($where)->count();
                $page = new \org\Page($count, 10,"","$write_string");
                $list = $news_info->where($where)->order("checkdate desc")->limit($page->firstRow, $page->listRows)->cache()->select();
                $this->assign("page", trim($page->show()));
                $this->assign("list", $list);


                //SEO优化，生成TITLE,K,D
                $from = array('，', '{zone_name}', '{cat_name}');
                $to = array(',', '', $cate['title']);

                $seo_title = str_replace($from, $to, C('site.zone_search_news_seotitle'));
                $seo_keywords = str_replace($from, $to, C('site.zone_search_news_seokeywords'));
                $seo_description = str_replace($from, $to, C('site.zone_search_news_seodescription'));
                $this->assign('seo_title', $seo_title);
                $this->assign('seo_keywords', $seo_keywords);
                $this->assign('seo_description', $seo_description);

                return $this->fetch();
        }

}
